package com.google.appengine.codelab;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

public class Sessao extends BaseKind {
	
	public int createOrUpdate(long ses_id, String cliente_id, long sis_id, long nav_id, 
			String ses_data, String ses_hora) {
		Entity ses = Datastore.get(getKindName(), ses_id);
		Entity sis = Datastore.get("SistemaOperacional", sis_id);
		Entity nav = Datastore.get("Navegador", nav_id);
		if ((sis != null) && (nav != null)) {
			if (ses == null) {
				ses = new Entity(getKindName(), ses_id);
			}
			ses.setProperty("cliente_id", cliente_id);
			ses.setProperty("sis_id", sis.getKey().getId());
			ses.setProperty("nav_id", nav.getKey().getId());
			ses.setProperty("ses_data", ses_data);
			ses.setProperty("ses_hora", ses_hora);
			Util.persistEntity(ses);
			return 0;
		}
		return -1;
	}
	
	public int delete(long ses_id) {
		return super.delete("Transacao", "ses_id", ses_id);
	}
	
	public PreparedQuery get(String data_ini, String data_fin) {
		Filter filter = null;
		List<Filter> f = new ArrayList<Filter>();
		if (data_ini != "") {
			f.add(new FilterPredicate("ses_data", FilterOperator.GREATER_THAN_OR_EQUAL,
					data_ini));
		}
		if (data_fin != "") {
			f.add(new FilterPredicate("ses_data", FilterOperator.LESS_THAN_OR_EQUAL,
					data_fin));
		}
		if (f.size() > 0) {
			filter = (f.size() == 1 ? f.get(0) : CompositeFilterOperator.and(f));
		}
		return this.get(filter);
	}
	
	public PreparedQuery getBySisId(long sis_id) {
		Filter filter = new FilterPredicate("sis_id", FilterOperator.EQUAL, sis_id);
		return this.get(filter);
	}
	
	public PreparedQuery getBySisId(long sis_id, String data_ini, String data_fin) {
		List<Filter> filter = new ArrayList<Filter>();
		filter.add(new FilterPredicate("sis_id", FilterOperator.EQUAL, sis_id));
		if (data_ini != "") {
			filter.add(new FilterPredicate("ses_data", FilterOperator.GREATER_THAN_OR_EQUAL,
					data_ini));
		}
		if (data_fin != "") {
			filter.add(new FilterPredicate("ses_data", FilterOperator.LESS_THAN_OR_EQUAL,
					data_fin));
		}
		return this.get(filter.size() == 1 ? filter.get(0) : CompositeFilterOperator.and(
				filter));
	}
	
	public PreparedQuery getBySisAndNavIds(long sis_id, long nav_id) {
		Filter f1 = new FilterPredicate("sis_id", FilterOperator.EQUAL, sis_id);
		Filter f2 = new FilterPredicate("nav_id", FilterOperator.EQUAL, nav_id);
		Filter fc = CompositeFilterOperator.and(f1, f2);
		return this.get(fc);
	}
	
	public PreparedQuery getBySisAndNavIds(long sis_id, long nav_id, String data_ini,
			String data_fin) {
		List<Filter> filter = new ArrayList<Filter>();
		filter.add(new FilterPredicate("sis_id", FilterOperator.EQUAL, sis_id));
		filter.add(new FilterPredicate("nav_id", FilterOperator.EQUAL, nav_id));
		if (data_ini != "") {
			filter.add(new FilterPredicate("ses_data", FilterOperator.GREATER_THAN_OR_EQUAL,
					data_ini));
		}
		if (data_fin != "") {
			filter.add(new FilterPredicate("ses_data", FilterOperator.LESS_THAN_OR_EQUAL,
					data_fin));
		}
		return this.get(filter.size() == 1 ? filter.get(0) : CompositeFilterOperator.and(
				filter));
	}
	
	public PreparedQuery getByNavId(long nav_id) {
		Filter filter = new FilterPredicate("nav_id", FilterOperator.EQUAL, nav_id);
		return this.get(filter);
	}

}
